<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GrPPI: Divide/conquer pattern</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">0.3.1</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Divide/conquer pattern </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <b>divide/conquer</b> pattern splits a problem into two or more independent subproblems until a base case is reached. The base case is solved directly and the results of the subproblems are combined until the final solution of the original problem is obtained.</p>
<p>The interface to the <b>Divide&amp;Conquer</b> pattern is provided by function <code><a class="el" href="group__divide__conquer__pattern.html#gac2ebc10ffc0be3d2f8170e621dbdbca2" title="Invoke Divide/conquer pattern.  Execution Execution type. ">grppi::divide_conquer()</a></code>. As all functions in <em>GrPPI</em>, this function takes as its first argument an execution policy.</p>
<div class="fragment"><div class="line"><a class="code" href="group__divide__conquer__pattern.html#gac2ebc10ffc0be3d2f8170e621dbdbca2">grppi::divide_conquer</a>(exec, other_arguments...);</div></div><!-- fragment --><h2>Divide/conquer variants</h2>
<p>There is a single variant:</p>
<ul>
<li><b>Generic problem divide/conquer</b>: Applies the <em>divide/conquer</em> pattern to a generic problem and returns a solution.</li>
</ul>
<h2>Key elements in divide/conquer</h2>
<p>The key elements of the <b>divide/conquer</b> pattern are: a <b>Divider</b> operation that divides a problem into subproblems, a <b>Solver</b> operation that is used to solve a subproblem, and a <b>Combiner</b> operation that is used to merge results of subproblems.</p>
<p>A <b>Divider</b> is any C++ callable entity that takes a problem and returns a collection of subproblems. The returned collection of subproblems must be iterable. This allows returning any standard C++ sequence container, or even a plain array. When a problem cannot be divided into subproblems, the divider returns a collection with a single subproblem.</p>
<p>The <b>Solver</b> is any C++ callable entity that takes a problem and turns it into a solution. The signature of the solver takes as argument a problem and returns the corresponding solution.</p>
<p>The <b>Combiner</b> is any C++ callable entity capable to combine two solutions. The signature of the combiner takes two solutions and returns a new combined solution.</p>
<h2>Details on divide/conquer variants</h2>
<h3>Generic divide/conquer</h3>
<p>The <b>divide/conquer</b> pattern takes an input problem and generates an output problem. </p><hr/>
<p> <b>Example</b>: Merge sort of an array. </p><div class="fragment"><div class="line">vector&lt;int&gt; v{1,3,5,7,2,4,6,8};</div><div class="line"></div><div class="line"><span class="keyword">struct </span>range {</div><div class="line">  std::vector&lt;int&gt;::iterator first, last;</div><div class="line">  <span class="keyword">auto</span> size()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> distance(first,last); }</div><div class="line">};</div><div class="line"></div><div class="line">std::vector&lt;range&gt; divide(range r) {</div><div class="line">  <span class="keyword">auto</span> mid = r.first + distance(r.first,r.last)/2;</div><div class="line">  <span class="keywordflow">return</span> { {r.first,mid} , {mid, r.last} };</div><div class="line">}</div><div class="line"></div><div class="line">range problem{begin(v), end(v)};</div><div class="line"></div><div class="line"><span class="keyword">auto</span> res = <a class="code" href="group__divide__conquer__pattern.html#gac2ebc10ffc0be3d2f8170e621dbdbca2">grppi::divide_conquer</a>(exec,</div><div class="line">  problem,</div><div class="line">  [](<span class="keyword">auto</span> r) -&gt; vector&lt;range&gt; {</div><div class="line">    <span class="keywordflow">if</span> (1&gt;=r.size()) { <span class="keywordflow">return</span> {r}; }</div><div class="line">    <span class="keywordflow">else</span> { <span class="keywordflow">return</span> divide(r); }</div><div class="line">  },</div><div class="line">  [](<span class="keyword">auto</span> x) { <span class="keywordflow">return</span> x; },</div><div class="line">  [](<span class="keyword">auto</span> r1, <span class="keyword">auto</span> r2) {</div><div class="line">    std::inplace_merge(r1.first, r1.last, r2.last);</div><div class="line">    <span class="keywordflow">return</span> range{r1.first, r2.last};</div><div class="line">  }</div><div class="line">);</div></div><!-- fragment --><hr/>
 </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
